package com.md.youyouhui.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityWelcomeBinding;
import com.md.youyouhui.net.Constants;
import com.md.youyouhui.net.response.ShowResponse;
import com.md.youyouhui.utils.SharedPreferencesUtils;
import com.md.youyouhui.utils.StatusBarHelper;
import com.md.youyouhui.view.MainViewModel;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by AdministratorZJ
 * on 2019/4/2.
 *
 * @author AdministratorZJ
 */
public class WelcomeActivity extends BaseActivity {
    private MainViewModel viewModel ;
    private ActivityWelcomeBinding welcomeBinding;
    private Timer timer = new Timer();
    private int time = 5;

    @Override
    public void setContentLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StatusBarHelper.setFullWindow(getWindow());
        }
        welcomeBinding = DataBindingUtil.setContentView(this, R.layout.activity_welcome);
    }

    @Override
    public void initView() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                time--;
                runOnUiThread(() -> {
                    welcomeBinding.timeWe.setText(String.format("%ds", time));
                    if (time == 0) {
                        timer.purge();
                        timer.cancel();
                        /*String token = SharedPreferencesUtils.getString(WelcomeActivity.this, Constants.USER_TOKEN, "");
                        if (TextUtils.isEmpty(token)) {
                            startActivity(new Intent(ct, LoginActivity.class));
                        } else {
                            startActivity(new Intent(ct, MainActivity.class));
                        }*/
                        startActivity(new Intent(ct, MainActivity.class));
                        finish();
                    }
                });
            }
        }, 10, 1000);

        AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
        //    设置Title的图标
        builder.setIcon(R.mipmap.app_icon);
        //    设置Title的内容
        builder.setTitle("隐私协议");
        //    设置Content来显示一个信息
        builder.setMessage("欢迎使用油有惠，请点击阅读并充分理解《隐私协议》和《用户协议》，为了提供更好的服务，我们在此特说明：\n" +
                "1：在您授权通过的情况下，我们将获取您的设别，位置等信息，您有权拒绝或者取消授权\n" +
                "2：系统的安全保障措施，会严格保护您的个人信息\n" +
                "3：未经您的授权，也不会与其分享您的信息\n" +
                "\n" +
                "\n" +
                "         ");
        //    设置一个PositiveButton
        builder.setPositiveButton(" （知道了）", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                //  Toast.makeText(MainActivity.this, "positive: " + which, Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    private void getIsShow() {
        viewModel.getShow().observe(this, showResponseResource -> {
            assert showResponseResource != null;
            switch (showResponseResource.status) {

                case SUCCESS:
                    ShowResponse data = showResponseResource.data;

                    SharedPreferencesUtils.saveString(this, Constants.IS_SHOW, data.isShow);
                    break;

            }
        });
    }

    @Override
    public void initData() {
        welcomeBinding.timeWe.setOnClickListener(v -> {
            timer.purge();
            timer.cancel();
            /*String token = SharedPreferencesUtils.getString(this, Constants.USER_TOKEN, "");
            if (TextUtils.isEmpty(token)) {
                startActivity(new Intent(ct, LoginActivity.class));
            } else {
                startActivity(new Intent(ct, MainActivity.class));
            }*/
            startActivity(new Intent(ct, MainActivity.class));
            finish();
        });
        viewModel = new MainViewModel();
        getIsShow();
    }


}
